
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ScrewTurn.Wiki.PluginFramework;

namespace ScrewTurn.Wiki {

	public partial class UserProfile : BasePage {

		#region Initialization
		
		protected void Page_Load(object sender, EventArgs e) {
			Page.Title = Resources.Messages.ProfileTitle + " - " + Settings.WikiTitle;

			if(SessionFacade.LoginKey == null) {
				Response.Redirect("Login.aspx?Redirect=Profile.aspx");
			}

			if(SessionFacade.Username.Equals("admin")) {
                lblDescription.Text = Resources.Messages.LoggedAsAdmin;
                lblAccountType.Text = "Administrator";
                lblUsername.Visible = false;
                lblInfo.Visible = false;
                txtEmail1.Enabled = false;
                txtEmail2.Enabled = false;
                txtOldPassword.Enabled = false;
                txtPassword1.Enabled = false;
                txtPassword2.Enabled = false;
                btnSaveEmail.Enabled = false;
                btnSavePassword.Enabled = false;
				btnDeleteAccount.Enabled = false;
                return;
            }

            if(!Page.IsPostBack) {
				lblUsername.Text = SessionFacade.Username;
				if(SessionFacade.Admin) {
                    lblAccountType.Text = "Administrator";
                }
                else {
                    lblAccountType.Text = "User";
                }
				Users u = Users.Instance;
				UserInfo user = u.Find(SessionFacade.Username);
				txtEmail1.Text = user.Email;

				LoadRegExp();
				LoadValidationLocalizations();
			}
		}

		private void LoadRegExp() {
			//User 
			rxvEmail1.ValidationExpression = Settings.EmailRegex;
			rxvPassword1.ValidationExpression = Settings.PasswordRegex;
		}

		private void LoadValidationLocalizations() {
			//User validators
			rfvEmail1.ErrorMessage = Resources.Messages.RequiredField;
			rfvEmail1.ToolTip = Resources.Messages.RequiredField;
			rfvEmail2.ErrorMessage = Resources.Messages.RequiredField;
			rfvEmail2.ToolTip = Resources.Messages.RequiredField;
			rfvOldPassword.ErrorMessage = Resources.Messages.RequiredField;
			rfvOldPassword.ToolTip = Resources.Messages.RequiredField;
			rfvPassword1.ErrorMessage = Resources.Messages.RequiredField;
			rfvPassword1.ToolTip = Resources.Messages.RequiredField;
			rfvPassword2.ErrorMessage = Resources.Messages.RequiredField;
			rfvPassword2.ToolTip = Resources.Messages.RequiredField;

			rxvEmail1.ErrorMessage = Resources.Messages.InvalidEmail;
			rxvEmail1.ToolTip = Resources.Messages.InvalidEmail;
			rxvPassword1.ErrorMessage = Resources.Messages.InvalidPassword;
			rxvPassword1.ToolTip = Resources.Messages.InvalidPassword;

			cvEmail1.ErrorMessage = Resources.Messages.EmailsNotEqual;
			cvEmail1.ToolTip = Resources.Messages.EmailsNotEqual;
			cvEmail2.ErrorMessage = Resources.Messages.EmailsNotEqual;
			cvEmail2.ToolTip = Resources.Messages.EmailsNotEqual;

			cvOldPassword.ErrorMessage = Resources.Messages.WrongPassword;
			cvOldPassword.ToolTip = Resources.Messages.WrongPassword;
			cvPassword1.ErrorMessage = Resources.Messages.PasswordsNotEqual;
			cvPassword1.ToolTip = Resources.Messages.PasswordsNotEqual;
			cvPassword2.ErrorMessage = Resources.Messages.PasswordsNotEqual;
			cvPassword2.ToolTip = Resources.Messages.PasswordsNotEqual;
		}
		
		#endregion
		
		protected void btnSaveEmail_Click(object sender, EventArgs e) {
			if(!Page.IsValid) {
				return;
			} 

			Users u = Users.Instance;
			UserInfo user = u.Find(SessionFacade.Username);
			u.ChangeEmail(user, txtEmail1.Text);
			lblSaveEmailResult.CssClass = "resultok";
			lblSaveEmailResult.Text = Resources.Messages.EmailSaved;
			txtEmail2.Text = "";
		}

		protected void btnSavePassword_Click(object sender, EventArgs e) {
			rfvOldPassword.Validate();
			rfvPassword1.Validate();
			rfvPassword2.Validate();
			cvOldPassword.Validate();
			cvPassword1.Validate();
			cvPassword2.Validate();
			rxvPassword1.Validate();

			if (!Page.IsValid) {
				return;
			}
			Users u = Users.Instance;
			UserInfo user = u.Find(SessionFacade.Username);

			u.ChangePassword(user, txtPassword1.Text);
			lblSavePasswordResult.CssClass = "resultok";
			lblSavePasswordResult.Text = Resources.Messages.PasswordSaved;
			txtOldPassword.Text = "";
			txtPassword1.Text = "";
			txtPassword2.Text = "";
		}

		protected void btnDeleteAccount_Click(object sender, EventArgs e) {
			btnConfirm.Enabled = true;
			btnDeleteAccount.Enabled = false;
		}

		protected void btnConfirm_Click(object sender, EventArgs e) {
			Log.LogEntry("Account deletion requested", EntryType.General, SessionFacade.Username);
			UserInfo user = Users.Instance.Find(SessionFacade.Username);
			Users.Instance.RemoveUser(user);
			Session.Abandon();
			Response.Redirect(Settings.DefaultPage + Settings.PageExtension);
		}

		#region Custom Validators
		
		protected void cvEmail1_ServerValidate(object source, ServerValidateEventArgs args) {
			args.IsValid = (string.Compare(txtEmail1.Text, txtEmail2.Text, true) == 0);
		}
		
		protected void cvEmail2_ServerValidate(object source, ServerValidateEventArgs args) {
			args.IsValid = true;
		}
		
		protected void cvPassword1_ServerValidate(object source, ServerValidateEventArgs args) {
			args.IsValid = txtPassword1.Text.Equals(txtPassword2.Text);
		}
		
		protected void cvPassword2_ServerValidate(object source, ServerValidateEventArgs args) {
			args.IsValid = true;
		}
		
		protected void cvOldPassword_ServerValidate(object source, ServerValidateEventArgs args) {
			UserInfo user = Users.Instance.Find(SessionFacade.Username);
			args.IsValid = user.Provider.TestAccount(user, txtOldPassword.Text);
		}
		
		#endregion
	}

}
